import 'package:flutter/material.dart';
import 'package:kxy_flutter_app/model/exam_category.dart';
import 'package:kxy_flutter_app/r.dart';
import 'package:line_awesome_icons/line_awesome_icons.dart';
import '../cate_exam_list_view.dart';
import '../const_cate_view.dart';
import '../test_list_view.dart';

class CateExamPanelWidget extends StatelessWidget {
  final String title;
  final String subtitle;
  final String imgName;
  final List<ExamCategory> cateList;

  const CateExamPanelWidget({Key key, this.title, this.subtitle, this.imgName, this.cateList}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return panel(context);
  }

  Widget panel(BuildContext context) {
    return Column(
      children: [
        Container(
          padding: EdgeInsets.symmetric(horizontal: 15, vertical: 15),
          decoration: panelDecoration(Colors.white, withTopRadius: true),
          child: Column(
            children: [
              header(),
              cateListWidget(context),
            ],
          ),
        ),
      ],
    );
  }

  Decoration panelDecoration(Color color, {bool withTopRadius = false}) {
    return BoxDecoration(
      color: color,
      borderRadius: BorderRadius.vertical(
        top: Radius.circular(withTopRadius ? 12 : 0),
        bottom: Radius.circular(12),
      ),
      boxShadow: [
        BoxShadow(
          blurRadius: 3,
          offset: Offset(1, 0),
          color: Color.fromRGBO(225, 225, 225, 1),
        ),
      ],
    );
  }

  Widget cateListWidget(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(vertical: 10),
      child: Column(
        children: ListTile.divideTiles(
          tiles: cateList.map((e) => cateItemWidget(e, context)),
          context: context,
          color: Color.fromRGBO(240, 240, 240, 1),
        ).toList(),
      ),
    );
  }

  Widget cateItemWidget(ExamCategory cate, BuildContext context) {
    return ListTile(
      contentPadding: EdgeInsets.all(0),
      leading: Container(
        height: 40,
        width: 40,
        alignment: Alignment.center,
        child: Image.asset(
          getImgName(cate),
          height: 26,
        ),
      ),
      title: Text(
        cate.name,
        style: TextStyle(
          fontSize: 16,
          color: Colors.black87,
        ),
      ),
      subtitle: Text(
        '上士闻道勤而行之。',
        style: TextStyle(
          fontSize: 12,
          color: Colors.black54,
        ),
      ),
      trailing: Icon(
        LineAwesomeIcons.angle_right,
        size: 20,
        color: Colors.black26,
      ),
      onTap: () {
        Navigator.of(context).push(
          MaterialPageRoute(builder: (cxt) {
            if (cate.type == 1) {
              return CategoryExamListView(category: cate);
            } else if (cate.type == 2) {
              return TestListView(category: cate);
            }
            return ConstCateView();
          }),
        );
      },
    );
  }

  Widget header() {
    return Container(
      height: 60,
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              Text(
                title,
                style: TextStyle(
                  fontSize: 20,
                  color: Colors.black,
                  fontWeight: FontWeight.bold,
                ),
              ),
              Text(
                subtitle,
                style: TextStyle(
                  fontSize: 14,
                  color: Colors.black54,
                ),
              ),
            ],
          ),
          Image.asset(imgName, height: 60),
        ],
      ),
    );
  }

  String getImgName(ExamCategory cate) {
    if (cate.type == 1) {
      if (cate.name.contains('正式')) return R.assetsExamExamOfficial;
      if (cate.name.contains('模拟')) return R.assetsExamExamSimulate;
      if (cate.name.contains('违')) return R.assetsExamExamBreak;
      if (cate.name.contains('安全')) return R.assetsExamExamSafe;
      if (cate.name.contains('月')) return R.assetsExamExamMonth;

      return R.assetsExamExamCommon;
    } else {
      if (cate.name.contains('每日一题')) return R.assetsExamTestDayOne;
      if (cate.name.contains('提升')) return R.assetsExamTestLevel;
      if (cate.name.contains('提高')) return R.assetsExamTestLevel;
      if (cate.name.contains('专项')) return R.assetsExamTestSpe;
      if (cate.name.contains('专业')) return R.assetsExamTestSpe;
      if (cate.name.contains('挑战')) return R.assetsExamTestChallenge;
      if (cate.name.contains('练习')) return R.assetsExamTestExercise;
      if (cate.name.contains('日')) return R.assetsExamTestDay;
      if (cate.name.contains('周')) return R.assetsExamTestWeek;
      if (cate.name.contains('月')) return R.assetsExamTestDay;

      return R.assetsExamTestCommon;
    }
  }
}
